﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ImportSubscriber
{
    public partial class frmConnectionStringWizard : Form
    {
        public string ConnectionString = "";

        public frmConnectionStringWizard()
        {
            InitializeComponent();
        }

        private void cb_integrated_security_CheckedChanged(object sender, EventArgs e)
        {
            tb_username.Enabled = !cb_integrated_security.Checked;
            tb_password.Enabled = !cb_integrated_security.Checked;
            content_Changed(null, null);
        }

        private void btn_connection_test_Click(object sender, EventArgs e)
        {
            SqlConnection conn = null;
            try
            {
                content_Changed(null, null);
                conn = new SqlConnection(tb_connection_string.Text);
                conn.Open();
                if (conn.State != ConnectionState.Open)
                {
                    MessageBox.Show("Connect database error.");
                    return;
                }
                MessageBox.Show("Connect database successfully.");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Connect database error." + ex.ToString());
                return;
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }
            }
        }

        private void content_Changed(object sender, EventArgs e)
        {
            if (cb_integrated_security.Checked)
                tb_connection_string.Text = String.Format(/*Provider=SQLOLEDB;*/ @"Data Source={0};Integrated Security=SSPI;Initial Catalog={1}", tb_host.Text, tb_database.Text);
            else
                tb_connection_string.Text = String.Format(/*Provider=SQLOLEDB;*/ @"Data Source={0};Initial Catalog={1};User ID={2};Password={3}", tb_host.Text, tb_database.Text, tb_username.Text, tb_password.Text);
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            content_Changed(null, null);
            ConnectionString = tb_connection_string.Text;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
